#! usr/bin/perl -w
use strict;

### load idcoeffile
sub LOADcorrespfile{
	my $filename=$_[0];
	my %f=();
	
	open(FILE, $filename) or die "cannot open $filename";
	while(my $line=<FILE>)
	{
		chomp($line);
		my($id,$num)=split(/ /,$line);

		$f{$num}{'id'}=$id;
	}
	close FILE;
	return(\%f);
}

####################################################################

sub main(){
	
	# collect arguments
	my $dataname=$ARGV[0];
	my $idcoeffile=$ARGV[1];
	my $correspfile=$ARGV[2];

	my $ref_corfile=&LOADcorrespfile($correspfile);
	my %corfile=%$ref_corfile;

	my $outputfile="ibdldreskin_$dataname";
	open(OUTPUTFILE,">$outputfile") or die "cannot open $outputfile";

	open(IDCOEFFILE, $idcoeffile) or die "cannot open $idcoeffile";
	while(my $line=<IDCOEFFILE>)
	{
		chomp($line);
		my @fields=split(/\t/,$line);

		# collect fields of interest
        	my $nb1=$fields[0];
		my $nb2=$fields[1];

		if((exists($corfile{$nb1})) && (exists($corfile{$nb2}))){
			print OUTPUTFILE "$dataname\t$corfile{$nb1}{'id'}\t$corfile{$nb2}{'id'}\t$fields[2]\t$fields[3]\t$fields[4]\t$fields[5]\t$fields[6]\t$fields[7]\t$fields[8]\t$fields[9]\t$fields[10]\n";
		}
		else{
			print "individual id doesn't exist.\n";
			exit;
		}	
	}
	close IDCOEFFILE;
	close OUTPUTFILE;

exit;
}

main();

